//
// Created by PC on 2023-9-1
// 二进制数相加
// https://leetcode.cn/problems/add-binary/submissions/
// 1 <= a.length, b.length <= 10^4
// leetcode 大佬的程序 4ms 6.09MB


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string addBinary(string a, string b)
{
    string sum;
    int t = 0;
    for(int i = a.size() - 1, j = b.size() - 1; i >= 0 || j >= 0; --i, --j)
    {
        if(i >= 0) t += a[i] - '0';
        if(j >= 0) t += b[j] - '0';
        sum.push_back((t % 2) + '0');
        t /= 2;
    }
    if(t) sum.push_back(1 + '0');
    reverse(sum.begin(), sum.end());
    return sum;

}

int main()
{
    string a = "1111001", b = "1001001";
    a="11",b="10";
//    a="0",b="0";
    cout<<addBinary(a,b);
    return 0;
}